package org.tnaggs.project.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.tnaggs.project.manager.ContactManager;
import org.tnaggs.project.manager.LeagueManager;
import org.tnaggs.project.manager.TeamManager;
import org.tnaggs.project.model.Contact;
import org.tnaggs.project.model.Player;
import org.tnaggs.project.model.Team;
import org.tnaggs.project.security.ContactUser;
import org.tnaggs.project.view.PlayerView;
import org.tnaggs.project.view.TeamView;



@Controller
@RequestMapping("/team")
public class TeamController {

	@Autowired
	private LeagueManager laxManager;
	
	@Autowired
	private TeamManager teamManager;
	
	@Autowired
	private ContactManager conManager;
	
	@RequestMapping(value = "/addTeam.html", method = RequestMethod.POST)
    public String addContact(@RequestParam("id") long id, @ModelAttribute("team")
                            Team team, BindingResult result) {
         team.setLeagueID((int)id);
        System.out.println("After: " + team.getLeagueID());
        laxManager.createTeam(team);
        
        String leagueName = laxManager.getLeagueRepo().findByID(team.getLeagueID()).getEncodedName();
        
         
        return "redirect:/league/view.html?name=" + leagueName;
    }


	@RequestMapping(value = "/view.*", method = RequestMethod.GET)
	public ModelAndView viewTeam(@RequestParam("name") String teamName) {
		 ModelAndView mav = new ModelAndView("viewTeam");
		 
		 String realName= teamName;
		 try {
			realName = URLDecoder.decode(teamName, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			System.err.println("Error decoding team name " + e);
		}
		 
		 Team team = laxManager.getTeamRepo().findByName(realName);
		 Contact coach = teamManager.getCoach(team.getContactID());
		 TeamView teamView = new TeamView();
		 teamView.setTeam(team);
		 teamView.setCoach(coach);
		 mav.addObject("team", teamView);
		 
		 List<PlayerView> players = new ArrayList<PlayerView>();
		 for(Player player : teamManager.getPlayers(team.getTeamID())) {
			 PlayerView playerV = new PlayerView();
			 playerV.setPlayer(player);
			 playerV.setContact(conManager.getContact(player.getContactID()));
			 playerV.setTeam(teamManager.getTeam(player.getTeamID()));
			 players.add(playerV);
		 }
		 
		 mav.addObject("players",players);
		 mav.addObject("contacts", conManager.getAllContacts());
		 
		 mav.addObject("commandPlayer", new Player());
		 List<String> positions = new ArrayList<String>();
		 positions.add("Attacker");
		 positions.add("Midfielder");
		 positions.add("Defenseman");
		 positions.add("Goalie");
		 
		 mav.addObject("positions", positions);
		 
		 ContactUser user = null;
	        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
	        if(principal instanceof UserDetails){
	          user = (ContactUser)principal;
	        } 
	        if(user != null) {
	          mav.addObject("principal", user.getContact());
	        } else {
	          mav.addObject("principal", "anonymous");	
	        }
		 
		 return mav;
	}
	
	public LeagueManager getLaxManager() {
		return laxManager;
	}

	public void setLaxManager(LeagueManager laxManager) {
		this.laxManager = laxManager;
	}

	public TeamManager getTeamManager() {
		return teamManager;
	}

	public void setTeamManager(TeamManager teamManager) {
		this.teamManager = teamManager;
	}
	
	
}
